<%@ page import="model.subjects.Person, database.dao.PersonDAO, model.Attribute, java.util.LinkedList, java.util.Map" language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="UTF-8" %>
<jsp:useBean id="errors" scope="request" type="java.util.Map"/>
<jsp:useBean id="personForm" scope="request" type="model.PersonForm"/>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Isiku lisamine</title>
</head>
<a href='http://imbi.ld.ttu.ee/tomcat_webapp_logs/t103755_subjekt/log.txt'>loggid</a> ||<a href="LoginServlet?action=logout">Logi välja</a> 


<ul>
<a href='LoginServlet?action=login'>Otsing</a> || 
<a href='s?action=display_add_person'>Lisa Isikuid</a> || 
<a href='s?action=display_add_enterprise'>Lisa ettevõtteid</a> || 
</ul>
<body><h1>Isiku lisamine</h1>
<%
String firstName="";
String lastName="";
String identityCode="";
String birthDate="";
String country="";
String county="";
String townVillage="";
String streetAddress="";
String zipcode="";
if(!errors.isEmpty()){
	firstName=personForm.getFirstName();
	lastName=personForm.getLastName();
	identityCode=personForm.getIdentityCode();
	birthDate=personForm.getBirthDate();
	country=personForm.getCountry();
	county=personForm.getCounty();
	townVillage=personForm.getTownVillage();
	streetAddress=personForm.getStreetAddress();
	zipcode=personForm.getZipcode();
}
//Teated:
String firstNameError="";
String lastNameError="";
String identityCodeError="";
String zipcodeError="";
if(!errors.isEmpty()){
	if(errors.containsKey("first_name")){
		firstNameError=(String)errors.get("first_name");
	}
	if(errors.containsKey("last_name")){
		lastNameError=(String)errors.get("last_name");
	}
	if(errors.containsKey("identity_code")){
	identityCodeError=(String)errors.get("identity_code");
	}
	if(errors.containsKey("zipcode")){
		zipcodeError=(String)errors.get("zipcode");
	}
}
%>
<form action='s?action=add_person' method='post' >
<!--  Üldised atribuudid -->
<table  bgcolor='#C0C0C0' ><tr><td>
<tr><td>Eesnimi </td><td><input name="first_name" type="text" value="<%out.println(firstName); %>"><% out.println(firstNameError);%><br/></td></tr>
<tr><td>Perenimi </td><td><input name="last_name" type="text" value="<%out.println(lastName);%>"><% out.println(lastNameError);%><br/></td></tr>
<tr><td>Isikukood </td><td><input name="identity_code" type="text" value="<%out.println(identityCode);%>"><% out.println(identityCodeError); %><br/></td></tr>
<tr><td>Sünnikuupäev </td><td><input name="birth_date" type="date" value="<%out.println(birthDate);%>"><br/></td></tr>
<br/>

<!-- Aadressi väljad -->
<tr><td></td><td>--------Aadressid------- <br/></td></tr>
<tr><td>Riik </td><td><input name="country" type="text" value="<%out.println(country);%>"><br/></td></tr>
<tr><td>Maakond </td><td><input name="county" type="text" value="<%out.println(county);%>"><br/></td></tr>
<tr><td>Linn/küla </td><td><input name="town_village" type="text" value="<%out.println(townVillage);%>"><br/></td></tr>
<tr><td>Tänav, maja, korter </td><td><input name="street_address" type="text" value="<%out.println(streetAddress);%>"><br/></td></tr>
<tr><td>Posti-indeks </td><td><input name="zipcode" type="number" value="<%out.println(zipcode);%>"><%out.println(zipcodeError); %><br/></td></tr>
<br/>

<%
//Isiku atribuudid
PersonDAO personDao = new PersonDAO();
LinkedList<Attribute> attributeNames = PersonDAO.findAttributeNames();
for(Attribute a: attributeNames){
	int dataType = a.getDataType();
	out.println("<tr><td><label>"+a.getName()+ " </label></td><td><input name=\"" + a.getName() + "\" type=\"");
	if(dataType == 1){
		out.println("text");
	}else if(dataType == 2){
		out.println("number");
	}else{
		out.println("date");
	}
	out.println("\" ");
	if(a.isRequired()){
		out.println("required");
	}
	out.println(" ><br/></td></tr>"); //inputi lõpp
	
}
%>
<tr><td> </td><td><input type="submit" value="Lisa"></tr></td></table>
</form>
</body>
</html>